package cn.hchaojie.ssm.web;

import java.util.Arrays;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping("/param/")
public class ParamController {
	// /param/request?id=1
	@RequestMapping("/request")
	public String request(HttpServletRequest request, HttpServletResponse response, HttpSession session) {
		// 从HtttServletRequest里面取请求参数
		String id = request.getParameter("id");
		System.out.println("id:" + id);
		
		System.out.println("response:" + response);
		System.out.println("session:" + session);

		return "success";
	}
	
	// model 是用来给视图层传数据的
	@RequestMapping("/model")
	public String model(HttpSession session, Model model) {
		
		model.addAttribute("user", "宝强");
		
		session.setAttribute("user", "宝强");
		
		return "success";
	}
	
	// /param/parameter?title=java		添加形参，形参的名字和请求参数的key对应
	@RequestMapping("/parameter")
	public String parameter(String title) {
		System.out.println("title:" + title);
		
		return "success";
	}
	
	// /param/parameter2?i=123&b=true&s=中&d=12.33&f=0.998&c=Z
	// 添加形参，形参的名字和请求参数的key对应
	// 代码混淆
	@RequestMapping("/parameter2")
	public String parameter2(char c, float f, double d, String s, boolean b, int i) {
		System.out.println("int:" + i);
		System.out.println("char:" + c);
		System.out.println("float:" + f);
		System.out.println("double:" + d);
		System.out.println("string:" + s);
		System.out.println("boolean:" + b);
		
		return "success";
	}
	
	// 如果形参的名字和请求参数的key不对应，使用@RequestParam来对应
	// /param/parameter3?string=abc
	@RequestMapping("/parameter3")
	public String parameter3(@RequestParam("string") String str) {
		System.out.println(str);
		
		return "success";
	}
	
	@RequestMapping("/array_form")
	public String parameter4() {
		return "array_form";
	}
	
	@RequestMapping(path="/array_form_submit", method = RequestMethod.POST)
	public String parameter5(
			@RequestParam("categories") int[] categories, 
			@RequestParam("posts") int[] posts) {
		System.out.println(Arrays.toString(categories));
		System.out.println(Arrays.toString(posts));

		return "success";
	}
}
